const {
  $util,
} = getApp();
const MAX_LENGTH = 10;
let isPrevImg = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typ: 0,
    page: 1,
    list: [],
    isPrev: true,
    isSearch: false,
    hasMore: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData(options);
  },
  onShow() {
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    })
    if (isPrevImg) {
      isPrevImg = false;
    }
    this.setData({
      page: 1,
    }, () => {
      this.getList();
    });
  },
  getList() {
    $util.request({
      url: 'entry/wxapp/group.topic.gettopcommentlist',
      data: {
        tid: this.data.id,
        page: this.data.page,
      },
      showLoading: false,
      success: (res) => {
        let list = res.data;
        if (this.data.page > 1) {
          list = [...this.data.list, ...list];
        }
        this.setData({
          list,
          topinfo: res.topinfo,
          hasMore: !((this.data.page !== 1 && res.data.length !== MAX_LENGTH)),
          loaded: true,
          page: res.data.length > 0 ? this.data.page + 1 : this.data.page,
        });
      },
    });
  },
  bindlike(e) {
    const id = e.currentTarget.dataset.id;
    const idx = e.currentTarget.dataset.idx;
    $util.request({
      url: 'entry/wxapp/group.comment.setlike',
      data: {
        commentid: id,
      },
      showLoading: false,
      success: (res) => {
        const list = this.data.list;
        list[idx].islike = Number(res.code);
        list[idx].likenum = Number(res.allnum);
        this.setData({
          list,
        });
      },
    });
  },
  onPrevImg() {
    isPrevImg = true;
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.data.hasMore && this.getList();
  },
});